#!/usr/bin/env python
'''AddNumber

Calls the AddNumber window
'''

import pygtk
pygtk.require("2.0")
import gtk

import Config
import Gui
import PhoneBook

class AddNumber(gtk.Dialog):
    '''The add number window'''
    def __init__(self, parent=None):
        '''Show the window'''
        gtk.Dialog.__init__(self, Config.get_config("title"), parent,
                gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                    (   gtk.STOCK_ADD, gtk.RESPONSE_ACCEPT,
                        gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL
                    )
                )
        
        # it can be called only from main window, so parent is that :)
        # Maybe I should look forward another solution... this one
        # is not really good
        self.main_w = parent
        self.set_size_request(300, 130)
        self.set_resizable(False)
        self.connect("destroy", self.windowDestroy)
        self.connect("response", self.windowHandleResponse)

        main_vbox = self.vbox
        main_vbox.set_homogeneous(False)
        self.windowSetup(main_vbox)

        self.show_all()


    def windowSetup(self, main_vbox):
        '''Setup the order of the components in the window'''
        label_name = gtk.Label(_("Name: "))
        label_number = gtk.Label(_("Number: "))
        label_comment = gtk.Label(_("Comment: "))

        entry_name = gtk.Entry()
        entry_number = gtk.Entry()
        entry_comment = gtk.Entry()

        self.entry_name = entry_name
        self.entry_number = entry_number
        self.entry_comment = entry_comment

        hbox_name = gtk.HBox()
        hbox_number = gtk.HBox()
        hbox_comment = gtk.HBox()

        hbox_name.pack_start(label_name, False, False, 5)
        hbox_name.pack_start(entry_name, True, True, 5)
        hbox_number.pack_start(label_number, False, False, 5)
        hbox_number.pack_start(entry_number, True, True, 5)
        hbox_comment.pack_start(label_comment, False, False, 5)
        hbox_comment.pack_start(entry_comment, True, True, 5)

        main_vbox.pack_start(hbox_name, False, False, 0)
        main_vbox.pack_start(hbox_number, False, False, 0)
        main_vbox.pack_start(hbox_comment, False, False, 0)


    def windowHandleResponse(self, wnd, response_id):
        '''Handle the response signal (AddNumber is gtkDialog window)'''
        if response_id == gtk.RESPONSE_CANCEL:
            self.hide()
        elif response_id == gtk.RESPONSE_ACCEPT:
            name = self.entry_name.get_text()
            number = self.entry_number.get_text()
            comment = self.entry_comment.get_text()

            e = False
            if name == "":
                e = True
                self.entry_name.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("#FFAFAF"))
                self.entry_name.connect("insert-text",
                        lambda *args:
                        self.entry_name.modify_base(gtk.STATE_NORMAL,  gtk.gdk.color_parse("white")))
            if number == "":
                e = True
                self.entry_number.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("#FFAFAF"))
                self.entry_number.connect("insert-text",
                        lambda *args:
                        self.entry_number.modify_base(gtk.STATE_NORMAL,  gtk.gdk.color_parse("white")))

            if e:
                Gui.ErrorBox(self, _("There are missing parameters!"))
            else:
                wnd.main_w.book.add_number(name, number, comment)
                wnd.main_w.windowUpdate()
                self.hide()


    def windowDestroy(self, window):
        '''Callback function called when the destroy signal is performed'''
        self.destroy()

